package com.bjsdzk.collector.timer;

import com.bjsdzk.collector.entity.onenet.DeviceInfoData;
import com.bjsdzk.collector.entity.onenet.DeviceInfoDevices;
import com.bjsdzk.collector.entity.onenet.OneNetResult;
import com.bjsdzk.collector.rpc.OneNetCollectService;
import io.vertx.core.json.JsonObject;
import io.vertx.redis.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Profile("pro")
public class OneNETTask {


    private static Logger logger = LoggerFactory.getLogger(OneNETTask.class);

    @Autowired
    private OneNetCollectService oneNetCollectService;
    @Autowired
    private RedisClient redis;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Scheduled(cron = "0 0,15,30,45 * * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void onenetTask() {
        onenetTask(0);
    }

    public void onenetTask(int page) {
        OneNetResult<DeviceInfoData> oneNetResult = oneNetCollectService.getAllDevice(page);
        DeviceInfoData data = oneNetResult.getData();
        int totalCount = data.getTotal_count();
        int perPage = data.getPer_page();
        int p = data.getPage();
        if (totalCount == perPage) {
            onenetTask(p + 1);
        }
        List<DeviceInfoDevices> devices = data.getDevices();
        List<String> result = devices.stream().map(m -> m.getId()).collect(Collectors.toList());
        result.forEach(s -> {
            Object imei = redisTemplate.boundHashOps(OneNetCollectService.ONENET_DEVICEID_IMEI).get(s);
            if(imei!=null) {
                JsonObject json = new JsonObject();
                json.put("imei", imei);
                json.put("deviceType", OneNetCollectService.ZHUOWEN);
                json.put("command", OneNetCollectService.GET_REALTIME);
                json.put("content", "{}");
                String cmd = oneNetCollectService.cmd(json.toString());
                logger.info("onenet task imei:{},result:{}", imei, cmd);
            }else {
                logger.error("onenet task imei:null,deviceId:{}",s);
            }
//            redis.hget(OneNetCollectService.ONENET_DEVICEID_IMEI, s, res -> {
//                if (res.succeeded()) {
//                    String imei = res.result();
//                    JsonObject json = new JsonObject();
//                    json.put("imei", imei);
//                    json.put("deviceType", OneNetCollectService.ZHUOWEN);
//                    json.put("command", OneNetCollectService.GET_REALTIME);
//                    json.put("content","");
//                    String cmd = oneNetCollectService.cmd(json.toString());
//                    logger.info("onenet task imei:{},result:{}",imei,cmd);
//                }
//            });
        });

    }

}
